home *** CD-ROM | disk | FTP | other *** search
/ ADA Programming Guide / ADA Programming Guide.iso / ada_gwu / set.h < prev    next >
C/C++ Source or Header  |  1996-01-30  |  4KB  |  124 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. #ifndef _set_h
  10. #define _set_h
  11. /*        Set and Tuple procedures
  12.             David Shields
  13.               27 Jan 84
  14.  */
  15.     
  16. typedef char **Tuple;
  17.  
  18. /*
  19. Tuples are represented as an array of pointers to the element values.
  20. Tuples of small integers can be maintained using casts to store
  21. integers instead of pointers in the array. The first element of the
  22. array gives the number of components.
  23.  
  24.   tup_add(tia,tib) concatenate two tuples
  25.   tup_copy(ti)    return copy of small tuple
  26.   tup_exp(ti,n) expand tuple to length n
  27.   tup_free(ti)    free tiace allocated for tuple
  28.   tup_frome(ti) remove element at end
  29.   tup_fromb(ti) remove element from front
  30.   tup_mem(n,tp) test for membership in tuple
  31.   tup_memi(n,tp) test for membership return index if found, else 0
  32.   tup_new(n)    make new tuple with length zero, no elements
  33.   tup_new1(a)    make new tuple with one component a (singleton)
  34.   tup_new2(a,b) make new tuple with two components: a and b (pair)
  35.   tup_print(ti) print small tuple on standard outiut (debug assist)
  36.   tup_size(ti)    return current size of small tuple
  37.   tup_with(ti,n) add element to end of tuple
  38.  
  39. */
  40.  
  41.  
  42. /* to iterate over Tuple, (Setl (for var in tup)use
  43.   SETL    (for var in tup) becomes:
  44.  
  45.   Fortup fort;
  46.   ...
  47.   FORTUP(var,tup, fort)
  48.    ...
  49.   ENDFORTUP(fort)
  50.  
  51.   FORTUPI is variant corresponding to SETL  (for var = tup(i))
  52. */
  53. /* Note that tup only evaluated once in FORTUP */
  54. #define FORTUP(var,tup,iv) \
  55.     iv.fortup_val = tup;\
  56.     if(iv.fortup_count = (int) iv.fortup_val[0]) {\
  57.     do { var *++iv.fortup_val;
  58. #define FORTUPI(var,tup,ndx,iv) \
  59.     iv.fortup_val = tup;\
  60.     if(iv.fortup_count = (int) iv.fortup_val[0]) {\
  61.     ndx = 0;\
  62.     do { ndx++; var *++iv.fortup_val; 
  63. #define ENDFORTUP(iv) }      while(--iv.fortup_count); } 
  64.  
  65. /* for iteration over tuples */
  66. typedef struct Fortup {
  67.   int     fortup_count;
  68.   char    ** fortup_val;
  69. } Fortup;
  70.  
  71. typedef char **Set;
  72.  
  73. /* apr 86 - modify to use tuples to represent sets */
  74. /* This package provides the following procedures:
  75.  
  76.  set_arb(sp)    pick arbitrary element from set
  77.  set_copy(sp)    return copy of small set
  78.  set_del(sp,n)    delete n from small set sp,return pointer to result
  79.  set_get(sp,n)    return n-th element of set
  80.  set_free(sp)    free space allocated for set
  81.  set_from(sp)    pick arbitrary element from set and remove it
  82.  set_init(n)    set up initial set space for n sets
  83.  set_less(sp1,n) remove element n from set sp1
  84.  set_new(n)    allocate new small set with room for n elements
  85.  set_new1(e)    allocate new small set with single element e
  86.  set_mem(n,sp)    see if n member of small set sp
  87.  set_print(sp)    print small set on standard output (debug assist)
  88.  set_size(sp)    return current size of small set
  89.  set_union(sp1,sp2) union of two small sets
  90.  set_with(sp,n) insert n in small set sp, return pointer to result
  91.  
  92.  to iterate over set, (Setl (for var in tup)use
  93.   SETL    (for var in tup) becomes:
  94.  
  95.   Forset fors;
  96.   ...
  97.   FORSET(var,tup, fors)
  98.    ...
  99.   ENDFORSET(fors)
  100.  
  101. */
  102.  
  103. /* variables used to control iteration over sets have types Forset */
  104.  
  105.  
  106. #define FORSET(var,tup,iv) \
  107.     iv.fortup_val = tup;\
  108.     if(iv.fortup_count = (int) iv.fortup_val[0]) {\
  109.     do { var *++iv.fortup_val;
  110. #define ENDFORSET(iv) }      while(--iv.fortup_count); } 
  111.  
  112. /* for iteration over sets (as tuples) */
  113. typedef struct Forset {
  114.   int     fortup_count;
  115.   char    ** fortup_val;
  116. } Forset;
  117.  
  118. #ifdef EXPORT
  119. #define tup_size(t)    (*(int *)(t))
  120. #define set_size(s)    (*(int *)(s))
  121. #endif
  122. #endif
  123.  
  124.